package com.example.zzbus.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.example.zzbus.R;
import com.example.zzbus.base.BaseActivity;
import com.example.zzbus.base.BaseApplication;
import com.example.zzbus.data.UserData;
import com.example.zzbus.db.User;
import com.example.zzbus.db.UserDao;

import baidumapsdk.demo.search.RoutePlanDemo;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.et_pwd)
    EditText etPwd;
    @Bind(R.id.et_nickname)
    EditText etNickname;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.reg_ok)
    Button regOk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.reg_ok)
    public void onViewClicked() {
        String username=etUsername.getText().toString();
        if (TextUtils.isEmpty(username)){
            showTost("请输入用户名");
            return;
        }
        String userpwd=etPwd.getText().toString();
        if (TextUtils.isEmpty(userpwd)){
            showTost("请输入密码");
            return;
        }
        String usernick=etNickname.getText().toString();
        if (TextUtils.isEmpty(usernick)){
            showTost("请输入昵称");
            return;
        }
        String userphone=etPhone.getText().toString();
        if (TextUtils.isEmpty(userphone)){
            showTost("请输入手机号码");
            return;
        }
        User user=new User();
        user.setUsername(username);
        user.setUserpwd(userpwd);
        user.setUsernick(usernick);
        user.setUserphone(userphone);
      long index=  BaseApplication.getmDaoSession().getUserDao().insert(user);
      if (index>-1){
          UserData.setBoolData(true);
          showTost("注册成功");
          UserData.setSpString(UserData.sp_userName,username);
          UserData.setSpString(UserData.sp_userNick,usernick);
          UserData.setSpString(UserData.sp_userPhone,userphone);
          startActivity(new Intent(context, RoutePlanDemo.class));
          finish();
      }
    }
}
